<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2019-04-08
 * Time: 15:32
 */

namespace app\api\controller;

use app\common\model\Video as VideoModel;
use app\common\model\VideoCate;
use app\common\model\VideoPay as VideoPayModel;
use app\common\model\User as UserModel;
use app\common\model\VideoPay;
use app\model\VideoCateModel;
use app\service\PayService;
use app\common\model\Live;
use app\common\model\Config;
use app\service\VideoPayService;
use app\service\VideoService;

class Video extends Base
{
    public $videoModel;
    public $videoCateModel;

    public function __construct()
    {
        parent::__construct();
        $this->videoModel = new \app\common\model\Video();
        $this->videoCateModel = new VideoCateModel();
    }

    /**
     *  首页课程列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function video_index()
    {
        $data = input('');
        $cate = $this->videoCateModel->order('ord desc')->field('id,name')->select();
        if ($cate) {
            $cate = $cate->toArray();
            foreach ($cate as $key => $value) {
                $where = [
                    'cate_id' => $value['id'],
                    'status' => 1
                ];
                $list = $this->videoModel->limit(4)->where($where)->order('ord desc,id asc')->select()->toArray();
                foreach ($list as $k => $val) {
                    $list[$k]['picture_src'] = tomedia($val['picture']);
                    $list[$k]['video_src'] = tomedia($val['video']);
                    $list[$k]['cate'] = VideoCate::getValue(['id'=>$val['cate_id']],'name');
                }
                $cate[$key]['list'] = $list;
            }
        }
        $this->ajax(200, '', $cate);
    }

    /**
     * 课程列表
     */
    public function video_list()
    {
        $data = input('');
        $where = [
            'a.status' => 1
        ];
        $page = max($data['page'], 1);
        $size = max($data['size'], 20);
        if ($data['cate_id']) {
            $where['a.cate_id'] = $data['cate_id'];
        } else {
            $cate_id = VideoCate::getValue([],'id','ord desc');
            $where['a.cate_id'] = $cate_id;
        }
        if ($data['title']) {
            $where['a.title'] = [
                'like' => "%{$data['title']}%"
            ];
        }
        $list = VideoService::S()->queryPage($where, $page, $size, 'a.*,b.name', 'a.ord desc,a.status desc');
        if ($list) {
            foreach ($list as $k => $v) {
                $list[$k]['picture_src'] = tomedia($v['picture']);
                $list[$k]['video_src'] = tomedia($v['video']);
            }
        }else{
            $list = [];
        }
        $total = VideoService::S()->queryCount($where);
        $this->ajax(200, '', $list);
    }

    /**
     * 课程详情
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function video_detail()
    {
        $data = input('');
        if ($data['id']) {
            $video = $this->videoModel->where('id', $data['id'])->find();
            if ($video) {
                $video = $video->toArray();
                $video['picture_src'] = tomedia($video['picture']);
                $video['video_src'] = tomedia($video['video']);
                if ($video['price'] > 0) {
                    $video['is_pay'] = VideoPay::getOne(['video_id' => $data['id'], 'uid' => $this->uid]) ? true : false;
                } else {
                    $video['is_pay'] = false;
                }
                $video['is_subscribe'] = \app\common\model\VideoSubscribe::getOne(['video_id' => $data['id'], 'uid' => $this->uid]) ? true : false;
                $this->ajax(200, '', $video);
            } else {
                $this->ajax(202, '课程不存在');
            }
        } else {
            $this->ajax(202, '课程不存在');
        }
    }


    /* 视频列表 */
    public function list()
    {
        $input = input();
        $page = max($input['page'], 1);
        $size = 20;
        $where = [];
        $list = VideoModel::getPage($where, $page, $size, 'ord desc,id desc');
        if (!$list) {
            $this->ajax(200, 'success', []);
        }
        foreach ($list as $k => $v) {
            $list[$k]['picture'] = tomedia($v['picture']);
            $list[$k]['video'] = tomedia($v['video']);
            $list[$k]['buy_status'] = VideoModel::payStatus($this->uid, $v['id']);
        }
        $this->ajax(200, 'success', $list);
    }

    /**
     * 购买课程
     */
    public function buy()
    {
        $input = input();
        $videoInfo = VideoModel::getOneById($input['id']);
        if (!$videoInfo || $videoInfo['price'] == 0) {
            $this->ajax(202, '课程不存在或者免费');
        }
        $status = VideoModel::payStatus($this->uid, $input['id']);
        if ($status) {
            $this->ajax(202, '已购买过');
        }
        $log = [
            'uid' => $this->uid,
            'video_id' => $input['id'],
            'mall_id' => $this->member['mall_id'],
            'order_sn' => create_order_sn($this->uid, 'VIDEO'),
            'pay_money' => $videoInfo['price']
        ];
        $res = VideoPayModel::create($log);
        if ($res->id) {
            $orderConfig = [
                'body' => '订阅课程:' . $videoInfo['title'],
                'out_trade_no' => $log['order_sn'],
                'total_fee' => $log['pay_money'] * 100,
                'trade_type' => 'JSAPI',
                //'openid' => UserModel::uid2openid($log['uid']),
                'openid' => $this->member['openid_main'],
                'notify_url' => request()->domain() . "/notify/video/mall_id/" . $this->mall_id,
            ];
            $jsapi = PayService::S(['mall_id' => 1])->wechatJssdk($orderConfig);
            $this->ajax(200, 'success', [
                'jsapi' => $jsapi
            ]);
        } else {
            $this->ajax(202, '请重试');
        }
    }

    /**
     * 订阅列表
     */
    public function video_pay_list()
    {
        $data = input('');
        $page = max($data['page'], 1);
        $size = max($data['size'], 20);
        $where = [
            'a.uid' => $this->uid,
            'a.status' => 1
        ];
        $list = VideoPayService::S()->queryPage($where, $page, $size, 'a.*,b.title,b.video,b.picture,b.type');
        foreach ($list as $key => $value) {
            $list[$key]['picture'] = tomedia($value['picture']);
        }
        $total = VideoPayService::S()->queryCount($where);
        $this->ajax(200, '', compact('list', 'total', 'page', 'size'));
    }

    /* 打开直播 */
    public function liveOn()
    {
        $input = input();
        $input['status'] = $input['status'] == 1 ? 1 : 0;
        if (!UserModel::isLiveManager($this->uid)) {
            $this->ajax(202, '无权开播');
        }
        Config::saveConfig([
            'aliyun_live_open' => $input['status']
        ]);
        if ($input['status'] == 0) {
            $this->ajax(200, '已关闭直播', $data);
        }
        $info = Live::getLiveInfo($input['time']);
        $data = [
            'push' => $info['push']
        ];
        $this->ajax(200, '已打开直播', $data);
    }

    /**
     * 视频分类
     */
    public function video_cate(){
        if(request()->isPost()){
            $data = input('');
            $all_cate = [];
            $cate = VideoCate::getAll([],'ord desc,id desc','id,name');
            $cate = array_merge($all_cate,$cate);

            return returnJson(200,'success',$cate);
        }
    }
}



























